{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "h3Nuf-G4xJ0u"
      },
      "source": [
        "##### Copyright 2019 The TensorFlow Authors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "zZ81_4tLxSvd"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wNBP_f0QUTfO"
      },
      "source": [
        "# 开始使用 [TensorBoard.dev](https://tensorboard.dev)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "NEWs8JXRuGex"
      },
      "source": [
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://tensorflow.google.cn/tensorboard/tbdev_getting_started\">\n",
        "    <img src=\"https://tensorflow.google.cn/images/tf_logo_32px.png\" />\n",
        "    在 tensorFlow.google.cn 上查看</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs-l10n/blob/master/site/zh-cn/tensorboard/tbdev_getting_started.ipynb\">\n",
        "    <img src=\"https://tensorflow.google.cn/images/colab_logo_32px.png\" />\n",
        "    在 Google Colab 中运行</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://github.com/tensorflow/docs-l10n/blob/master/site/zh-cn/tensorboard/tbdev_getting_started.ipynb\">\n",
        "    <img src=\"https://tensorflow.google.cn/images/GitHub-Mark-32px.png\" />\n",
        "    在 GitHub 上查看源代码</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a href=\"https://storage.googleapis.com/tensorflow_docs/docs-l10n/site/zh-cn/tensorboard/tbdev_getting_started.ipynb\"><img src=\"https://tensorflow.google.cn/images/download_logo_32px.png\" />下载 notebook</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DLXZ3t1PWdOp"
      },
      "source": [
        "[TensorBoard.dev](https://tensorboard.dev) 提供了云端的 [TensorBoard](https://tensorflow.google.cn/tensorboard) 体验，可让您上传并与所有人共享 ML 实验结果。\n",
        "\n",
        "该笔记本训练了一个简单的模型，并演示了如何将日志上传到 TensorBoard.dev。"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yjBn-ptXTppA"
      },
      "source": [
        "### 配置并导入"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5C8BOea_rF49"
      },
      "outputs": [],
      "source": [
        "try:\n",
        "  # %tensorflow_version 仅存在于 Colab。\n",
        "  %tensorflow_version 2.x\n",
        "except Exception:\n",
        "  pass\n",
        "\n",
        "!pip install -U tensorboard >piplog 2>&1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "L3ns52Luracm"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "import datetime"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GqUABmUTT1Cl"
      },
      "source": [
        "### 训练一个简单的模型并创建 TensorBoard 日志"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "LZExSr2Qrc5S"
      },
      "outputs": [],
      "source": [
        "mnist = tf.keras.datasets.mnist\n",
        "\n",
        "(x_train, y_train),(x_test, y_test) = mnist.load_data()\n",
        "x_train, x_test = x_train / 255.0, x_test / 255.0\n",
        "\n",
        "def create_model():\n",
        "  return tf.keras.models.Sequential([\n",
        "    tf.keras.layers.Flatten(input_shape=(28, 28)),\n",
        "    tf.keras.layers.Dense(512, activation='relu'),\n",
        "    tf.keras.layers.Dropout(0.2),\n",
        "    tf.keras.layers.Dense(10, activation='softmax')\n",
        "  ])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "dsVjm5CrUtXm"
      },
      "outputs": [],
      "source": [
        "model = create_model()\n",
        "model.compile(optimizer='adam',\n",
        "              loss='sparse_categorical_crossentropy',\n",
        "              metrics=['accuracy'])\n",
        "\n",
        "log_dir=\"logs/fit/\" + datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\")\n",
        "tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)\n",
        "\n",
        "model.fit(x=x_train, \n",
        "          y=y_train, \n",
        "          epochs=5, \n",
        "          validation_data=(x_test, y_test), \n",
        "          callbacks=[tensorboard_callback])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "TgF35qdzIC3T"
      },
      "source": [
        "### 上传到 TensorBoard.dev\n",
        "\n",
        "上传 TensorBoard 日志将提供一个可以与任何人共享的链接。 请注意，上传的 TensorBoard 是公开的。 不要上传敏感数据。\n",
        "\n",
        "上传程序将一直运行直到停止，以便在进行训练时从目录中读取新数据。"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "n2PvxhOkW7vn"
      },
      "outputs": [],
      "source": [
        "!tensorboard dev upload --logdir ./logs"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5QH5k4AUNE27"
      },
      "source": [
        "请注意，您上传的每个实例都有一个唯一的实例 ID。 即使您使用相同的目录重新上传，您也会获得新的实例 ID。 可以使用 `list` 命令查看您上传的实例列表。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "C2Pj3RQCNQvP"
      },
      "outputs": [],
      "source": [
        "!tensorboard dev list"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JcZOGmjQNWk_"
      },
      "source": [
        "要删除已上传的实例，可以使用 `delete` 命令并指定适当的实例。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "VSkJTT9rNWJq"
      },
      "outputs": [],
      "source": [
        "# 您必须将 YOUR_EXPERIMENT_ID 替换为前一个输出的值\n",
        "# tensorboard `list` 命令或 `upload` 命令。 例如\n",
        "# `tensorboard dev delete --experiment_id pQpJNh00RG2Lf1zOe9BrQA`\n",
        "\n",
        "## !tensorboard dev delete --experiment_id YOUR_EXPERIMENT_ID_HERE"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "tbdev_getting_started.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
